package com.youdianstar.app.ui.reward

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.gyf.immersionbar.ImmersionBar
import com.safframework.eventbus.EventBus
import com.youdianstar.app.R
import com.youdianstar.app.BR
import com.youdianstar.app.common.base.BaseActivity
import com.youdianstar.app.common.ex.setThrottleListener
import com.youdianstar.app.common.network.BaseApiResultObserver
import com.youdianstar.app.common.network.BaseApiStateObserver

import com.youdianstar.app.adapter.PointPlanAdapter
import com.youdianstar.app.databinding.ActivityPointsPlanBinding
import com.youdianstar.app.entity.PointAccount
import com.youdianstar.app.entity.PointDbResult
import com.youdianstar.app.entity.PointPlan
import com.youdianstar.app.ui.pay.WalletActivity
import com.youdianstar.app.common.DataCode
import com.youdianstar.app.common.entity.BusEntity
import com.youdianstar.app.common.auko.startActivity
import com.youdianstar.app.ui.MainActivity
import org.koin.androidx.viewmodel.ext.android.viewModel


/**
 * File: PointPlanActivity
 * Author: 82149 Create: 2022/3/2 16:51
 * Changes (from 2022/3/2)
 * --------------------------------------------------
 * description: 积分规则
 * ---------------------------------------------------
 *
 */
class PointPlanActivity : BaseActivity<ActivityPointsPlanBinding, RewardViewModel>() {
    override fun layoutId(): Int = R.layout.activity_points_plan

    override fun initVariableId(): Int =  BR.viewModel

    override fun initViewModel(): RewardViewModel {
        val model: RewardViewModel by viewModel()
        return model
    }

    //积分余额
    private var pointBalance = 0
    private var mPlanAdapter: PointPlanAdapter? = null

    override fun initData() {
        super.initData()
        ImmersionBar.with(this)
            .statusBarDarkFont(true)
            .statusBarColor(R.color.color_E7FF55)
            .titleBarMarginTop(mBinding.lgInnerBg)
            .init()
        // 条件判断: 是否绑过卡?
        mPlanAdapter = PointPlanAdapter(this, mutableListOf(), object :PointPlanAdapter.IActionListener{
            override fun onItemAction(type: Int) {
                //1-新用户注册 2-每日访问APP 3-租借 4-绑卡 5-报失
                if (type == 3){
                    startActivity<MainActivity>()
                    finish()
                }else if (type == 4){
                    // 钱包功能已屏蔽，因为没有绑定银行卡的需求
                    // startActivity<WalletActivity>()
                }
            }
        })
        mBinding.rcRecord.layoutManager = LinearLayoutManager(this)
        mBinding.rcRecord.adapter = mPlanAdapter

        mBinding.swRecord.setOnChildScrollUpCallback(object : SwipeRefreshLayout.OnChildScrollUpCallback{
            override fun canChildScrollUp(parent: SwipeRefreshLayout, child: View?): Boolean {
                val linearLayoutManager = mBinding.rcRecord.layoutManager as LinearLayoutManager
                return linearLayoutManager.findFirstCompletelyVisibleItemPosition() != 0;
            }
        })
        mBinding.swRecord.setOnRefreshListener { mViewModel.getPointPlansInfo() }

        //获取积分账户
        mViewModel.getPointAccount()
        mViewModel.getPointPlansInfo()

        EventBus.post(BusEntity(DataCode.CLOSE_DRAWER))//关闭侧边栏
    }

    override fun initLiveObserver() {
        super.initLiveObserver()
        mViewModel.pointAccountLiveData.observe(this,object : BaseApiResultObserver<PointAccount>(){
            override fun onDataResult(data: PointAccount?) {
                super.onDataResult(data)
                data?.let {
                    pointBalance = it.point
                    mBinding.tvPoints.text = pointBalance.toString()
                }
            }
        })

        mViewModel.pointPlansLiveData.observe(this, object : BaseApiStateObserver<List<PointPlan>>(mBinding.rcRecord) {
                override fun onReload(v: View?) {
                    mViewModel.getPointPlansInfo()
                }

                override fun onDataResult(data: List<PointPlan>?) {
                    super.onDataResult(data)
                    mBinding.swRecord.isRefreshing = false
                    data?.let {
                        mPlanAdapter?.update(it)
                        mViewModel.checkPointsBindCard()
                    }
                }

                override fun onNetError(code: Int, e: String?) {
                    super.onNetError(code, e)
                    mBinding.swRecord.isRefreshing = false
                    toastShow(e)
                }
            })

        mViewModel.pointBindCardActionData.observe(this,object : BaseApiResultObserver<List<PointDbResult>>(true){
            override fun onDataResult(data: List<PointDbResult>?) {
                super.onDataResult(data)
                dismissDialog()
                //检查绑卡事件
                val isEnable = data.isNullOrEmpty()
                mPlanAdapter?.updatePlanItem(4, isEnable)
            }
        })
    }

    override fun initClickEvent() {
        mBinding.headBack.setThrottleListener {
            finish()
        }
        mBinding.btnRecordView.setThrottleListener {
            startActivity<PointRecordActivity>()
        }
    }
}